<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-25 20:19:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 20:22:43
 * @Descripttion: 
 */


namespace app\common\model;

use think\Model;

class AgentGoodsSku extends Base
{
    protected $insert = ['addtime','end_time'];

    //---------------自动完成----------------------
    protected function setAddtimeAttr()
    {
        return time();
    }
    protected function setEndTimeAttr($value,$data)
    {
       return (!empty($data['end_time'])) ? strtotime($data['end_time']) : 0 ;
    }
    // protected function setEndTimeAttr($value,$data)
    // {
    //    return (!empty($data['end_time'])) ? strtotime($data['end_time']) : 0 ;
    // }

    public function getEndTimeTextAttr($value, $data){
        return date('Y-m-d H:i:s', $data['end_time']);
    }
       /**
     * 获取图片数组
     *
     * @param [type] $value
     * @param [type] $data
     * @return void
     */
    public function getImgsAttr($value, $data)
    {
  
        $array = empty($data['imgs']) ? [] : json_decode($data['imgs'], true);
        return $array;
    }
    public function getContentAttr($value, $data)
    {
        $array = empty($data['content']) ? [] : json_decode($data['content'], true);
        return $array;
    }
 
    public function getStockAttr($value,$data){
      
       $goods_sku_id = (new \app\common\model\AgentGoodsSku())->where(['id' => $data['id']])->value('goods_sku_id');
       return (new \app\common\model\GoodsSku())->where(['id' => $goods_sku_id])->value('stock');
    }

    /**
     * 获取图片数组
     *
     * @param [type] $value
     * @param [type] $data
     * @return void
     */
    public function getCoverImageTextAttr($value, $data)
    {
        return cdnurl($data['cover_image'],true);
    }

    public function getSkuAttr($value,$data){
        return (new \app\common\model\GoodsSku())->field(['id','sku_title','money'])->where(['goods_id' => $data['id']])->select();
    }
    public function getBusUserNameAttr($value,$data){
        return (new \app\common\model\Bus())->where(['id' => $data['bus_id']])->value('user_name');
    }
    public function getAgentUserNameAttr($value,$data){
        return (new \app\common\model\Agent())->where(['id' => $data['agent_id']])->value('usename');
    }
   
    public function getBrandTitleAttr($value,$data){
        return (new \app\common\model\GoodsBrand())->where(['id' => $data['brand_id']])->value('title');
    }
    public function getCateTitleAttr($value,$data){
        return (new \app\common\model\GoodsCate())->where(['id' => $data['cate_id']])->value('title');
    }
//     public function getSalesAttr($value,$data){
//         $sku  = (new \app\common\model\AgentGoodsSku())->where(['id' => $data['id']])->find();
//         // $goods = (new \app\common\model\Goods())->where(['id' => $sku['goods_id']])->find();
//         // return $goods['z_sales']+$sku['sales']; 
//         return $sku['sales'];
//    }

}
